package yongxin.controller.tasks;

import com.alibaba.fastjson.JSON;
import io.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.BrowserUtils;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.ResourceUtil;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.jwt.util.ResponseMessage;
import org.jeecgframework.jwt.util.Result;
import org.jeecgframework.poi.excel.entity.TemplateExportParams;
import org.jeecgframework.poi.excel.export.template.ExcelExportOfTemplateUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.tag.vo.datatable.SortDirection;
import org.jeecgframework.web.system.pojo.base.TSUser;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import yongxin.entity.process.ProcessEntity;
import yongxin.entity.process.ProcessJsonEntity;
import yongxin.entity.process.ProcessNodeJsonEntity;
import yongxin.entity.processreport.ProcessReportEntity;
import yongxin.entity.tasks.TasksEntity;
import yongxin.service.process.ProcessServiceI;
import yongxin.service.processreport.ProcessReportServiceI;
import yongxin.service.tasks.TasksServiceI;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**   
 * @Title: Controller
 * @Description: 工序汇报任务单
 * @author zhangdaihao
 * @date 2015-02-05 11:44:08
 * @version V1.0   
 *
 */
@Controller
@RequestMapping("/tasks")
public class TasksRestController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(TasksRestController.class);

	@Autowired
	private TasksServiceI tasksService;
	@Autowired
	private ProcessServiceI processService;
	@Autowired
	private SystemService systemService;
	@Autowired
	private ProcessReportServiceI processReportService;
	



	/**
	 *
	 * 反下单
	 * @param fbcph
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/{fbcph}",method = RequestMethod.DELETE)
	@ResponseBody
	public ResponseMessage<?> cancel(@ApiParam(name = "fbcph", value = "本场批号", required = true) @PathVariable("fbcph") String fbcph, HttpServletRequest request)
	{

		AjaxJson j = new AjaxJson();

		// 获取任务单的id 、本厂批号和首道工序id

		//String fbcph = request.getParameter("fbcph");


		String sql = " select fstate from  t_processreport where id = ? " ;

		String sql2 = " delete from t_process where freportid = ? ";

		String sql3 = " delete from t_process where fbcph = ? and fprocess = 'p1' ";

		String sql1="select fid,fNextReportId from t_tasks where fbcph=? ";
		Map<String,Object> task=systemService.findOneForJdbc(sql1, fbcph);

		if(task==null ){

			return Result.error("本场批号不存在");
		}

		String fNextReportId=task.get("fNextReportId").toString();

		if (StringUtil.isNotEmpty(fNextReportId))
		{

			String fstate = String.valueOf(systemService.findOneForJdbc(sql, fNextReportId).get("fstate"));

			// 反下单之前 先判断首道工序完工否
			if (fstate.equals("2"))
			{

				return Result.error("反下单失败，该卷号在首道工序已完工");

			} else if (fstate.equals("4"))
			{


				return Result.error("反下单失败，该卷号在首道工序已装炉");

			}  else if (fstate.equals("3"))
			{

				return Result.error("反下单失败，该卷号在首道工序已分卷");

			}else   // 反下单
			{
				systemService.deleteEntityById(ProcessReportEntity.class, fNextReportId); // 删工序汇报记录
				systemService.deleteEntityById(TasksEntity.class, task.get("fid").toString()); // 删任务单

				systemService.executeSql(sql2, fNextReportId); // 删工序记录
				systemService.executeSql(sql3, fbcph);

				return Result.success("反下单成功");
			}

		} else
		{

			return Result.success("报工系统，数据异常！");
		}

	}


}
